package org.bigjava.buff.user.service.impl;

import org.apache.ibatis.annotations.Param;
import org.bigjava.buff.common.util.NotEmptyVerificationUtil;
import org.bigjava.buff.common.util.Page;
import org.bigjava.buff.goods.bean.ExteriorAndPrice;
import org.bigjava.buff.goods.bean.Type;
import org.bigjava.buff.goods.mapper.TypeMapper;
import org.bigjava.buff.user.bean.User;
import org.bigjava.buff.user.mapper.UserMapper;
import org.bigjava.buff.user.service.UserService;
import org.bigjava.buff.common.util.PwdEncryptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TypeMapper typeMapper;

    NotEmptyVerificationUtil util = new NotEmptyVerificationUtil();

    //登录
    public User login(User user) {

        //非空校验
        if(util.isEmptyTelPwd(user)){

            //用户未加密密码
            String userPwd = user.getPassword();

            //用户已加密密码
            String enUserPwd = PwdEncryptionUtil.shaEncode(userPwd);

            user.setPassword(null);
            //通过手机号查询该用户是否以注册
            User us = userMapper.getUser(user);
            if(us != null){
                System.out.println(us);
                //已注册 匹配密码是否相等
                if(us.getPassword().equals(enUserPwd)){
                    //登录成功
                    return us;
                } else{
                    //密码不匹配 登录失败
                    return null;
                }
            } else{
                //手机号未被注册 进行注册
                user.setPassword(enUserPwd);
                boolean res = this.register(user);
                if(res){
                    //注册成功
                    return userMapper.getUser(user);
                } else {
                    //注册失败 可能已有相同手机号
                    return null;
                }
            }
        }
        return null;
    }

    public boolean register(User user) {

        //非空对象非空字符
        boolean res = util.isEmptyTelPwd(user);
        if(res){
            //密码加密 加密后的密码
            String enPwd = PwdEncryptionUtil.shaEncode(user.getPassword());
            user.setPassword(enPwd);
            return userMapper.addUser(user);
        }
        return false;
    }


    public void updateUser(User user) {
        userMapper.updateUser(user);
    }


    /**
     *
     * @param userId    用户id
     * @param money     更新的金额
     * @param updateType    加或者减  -1减法  1加法
     * @return      失败 成功
     */
    public boolean updateBalance(Integer userId, Double money,Integer updateType) {
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("money",money);
        map.put("updateType",updateType);
        Integer res = userMapper.updateBalance(map);
        return res != null;
    }


    public User getUserbyId(Integer id){
        User user = new User();
        user.setId(id);
        return userMapper.getUser(user);
    }

    /**
     * 获取用户库存
     * @param id    用户id
     * @return List  用户库存物品集合
     */
    public Page getUserInventory(Integer id, Page page) {

        //获取数据总条数
        int total = this.getTotal(id);

        // 计算总页数
        page.setTotal(total);
        if(page.getPageNum() > page.getPages()) page.setPageNum(1);

        Map map = new HashMap();
        map.put("id", id);
        map.put("pageNum", (page.getPageNum() - 1) * page.getPageSize());
        map.put("pageSize", page.getPageSize());

        //获取所有一级类型
        List<Type> allTypeList = typeMapper.findtypeByfid(0);

        //获得库存集合
        List<ExteriorAndPrice> list = userMapper.getUserInventory(map);

        //填充属性
        for (ExteriorAndPrice e : list){
            Type type = e.getGoods().getGoodsType();
            for(Type t : allTypeList){
                if(type.getFid() == t.getId()){
                    type.setName(type.getTypeName());
                    type.setTypeName(t.getTypeName());
                }
            }
        }

        page.setList(list);
        return page;

    }
    //获取用户库存总条数
    public int getTotal(Integer id){
        return userMapper.getTotal(id);
    }

    public List<ExteriorAndPrice> findGoodsByIds(@Param("gid") List<Integer> gid, @Param("uid") Integer uid) {
        return userMapper.findGoodsByIds(gid,uid);
    }

    public int collectGoods(Integer goodsId, Integer uId) {
        return 0;
    }


}